
package com.example.actearly_revised;

import java.util.Arrays;

import com.example.actearly_revised.QuestionsObject.Node;
import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class UrgentlistPage extends ListActivity  {

	Node tempQuestion;
	SQLogic conn;
	String[] keys;
	QuestionsObject db;
	Node question_node;
	String milestoneNames[];
	TextView currentMilestone;
	ListView listview1;
	String question[];
	String ageGroups[];
	Spinner urgentAgeSpinner;
	ArrayAdapter<String> urgentAdapter;
	int[] ageID;
	static int milestone; 
	private CustomListAdapter listAdapter;

	public UrgentlistPage(){
	}

	@SuppressLint({ "NewApi", "NewApi" })
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		ageID = new int[] {
				2,4,6,9,12,18,24,36,48,60
		};
		super.onCreate(savedInstanceState);
		SQLogic conn = new SQLogic(UrgentlistPage.this);
		setContentView(R.layout.activity_urgentlist_page);

		milestone = QuicklistTabHomePage.currentmilestone;
		int CurrentAgeIndex = -1;
		for(int i = 0; i < ageID.length; i++){
			if(ageID[i] == milestone){
				CurrentAgeIndex = i;
				break;
			}
			CurrentAgeIndex = -1;
		}
		//font change
		/*Typeface customFont = Typeface.createFromAsset(getAssets(), "fonts/futuram.ttf");
				TextView Text = (TextView) findViewById(R.id.urgentlistSpinner);

				Text.setTypeface(customFont);*/


		conn.open();
		QuestionsObject db = conn.RetrieveQuestions(milestone,1,"u");
		conn.close();
		question= new String[db.count()];
		String keys[] = new String[db.count()];
		db.getMap().keySet().toArray(keys); 
		
//		keys aren't actually sorted in order, so need to do that here		
				int keyInt[]= new int[db.count()];

				for (int j=0; j<db.count();j++){
					keyInt[j]=Integer.parseInt(keys[j]);
				}
				Arrays.sort(keyInt);
				for (int j=0; j<db.count();j++){
					keys[j]=Integer.toString(keyInt[j]);
				}
				
		for (int j=0; j<db.count();j++)  // same as in UrgetList, autoincrement starts at 1 in table
		{
			Node question_node = db.getQuestions(keys[j]);
			question[j]= question_node.Question;
		}


		conn.close();   // should close DB as soon as your done with it
		//setListAdapter(new ArrayAdapter<String>(UrgentlistPage.this, android.R.layout.simple_list_item_1, question));
		ListView lv= (ListView) findViewById(android.R.id.list);
		lv.setAdapter(new CustomListAdapter(this,question));
		//setListAdapter(new CustomListAdapter(UrgentlistPage.this, R.layout.activity_list_layout, question));
		//listAdapter= (CustomListAdapter)getListAdapter();

		String[] items= getResources().getStringArray(R.array.milestoneAges);
		urgentAgeSpinner = (Spinner) findViewById(R.id.urgentlistSpinner);
		ArrayAdapter<String> urgentlistAdapter = new ArrayAdapter<String>(this,
				R.layout.activity_spinner_layout, items) {

			public View getView(int position, View convertView, ViewGroup parent) {
				View v = super.getView(position, convertView, parent);

				Typeface externalFont=Typeface.createFromAsset(getAssets(), "fonts/futuram.ttf");
				((TextView) v).setTypeface(externalFont);
				//((TextView) v).setTextColor(-1);


				return v;
			}

			public View getDropDownView(int position,  View convertView,  ViewGroup parent) {
				View v =super.getDropDownView(position, convertView, parent);

				Typeface externalFont=Typeface.createFromAsset(getAssets(), "fonts/futuram.ttf");
				((TextView) v).setTypeface(externalFont);

				return v;
			}
		};

		urgentlistAdapter.setDropDownViewResource(R.layout.activity_spinner_layout);
		urgentAgeSpinner.setAdapter(urgentlistAdapter);
		urgentAgeSpinner.setSelection(CurrentAgeIndex);


		urgentAgeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {

				int index = arg0.getSelectedItemPosition();
				// storing string resources into Array
				String name[] = getResources().getStringArray(R.array.milestoneAges);
				Log.d("switch","before switch");
				SQLogic conn = new SQLogic(UrgentlistPage.this);
				conn.open();
				QuestionsObject db = conn.RetrieveQuestions(ageID[index],1,"u");
				conn.close();
				question= new String[db.count()];
				String keys[] = new String[db.count()];
				db.getMap().keySet().toArray(keys); // this is needed because the keys might not necissarily be in order
				for (int j=0; j<db.count();j++)  // same as in UrgetList, autoincrement starts at 1 in table
				{
					Node question_node = db.getQuestions(keys[j]);
					question[j]= question_node.Question;
				}
				ListView lv= (ListView) findViewById(android.R.id.list);
				lv.setAdapter(new CustomListAdapter(UrgentlistPage.this,question));				
				milestone = ageID[index];
				QuicklistTabHomePage.currentmilestone = milestone;
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// do nothing
			}

		});     
	}

	@Override
	protected void onResume(){
		super.onResume();
		milestone = QuicklistTabHomePage.currentmilestone;

		// find the index of current age group
		int CurrentAgeIndex = -1;
		for(int i = 0; i < ageID.length; i++){
			if(ageID[i] == milestone){
				CurrentAgeIndex = i;
				break;
			}
			CurrentAgeIndex = -1;
		}

		urgentAgeSpinner.setSelection(CurrentAgeIndex);
	} 
	;}
